const app = getApp()
// pages/recommendedil/recommendedil.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    comintro: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    var  id  = options.jobdeil
    var uid = wx.getStorageSync('uid')
    that.setData({
      id:id
    })
    // 请求当前兼职信息
    wx.request({
      url: app.data.Url+"job/jobinfo",
      data: {
        bid: 51,
        id: id,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res,'dangqianjianzhi ');
        that.setData({
          res: res.data.data,
          comintro: true,
        })
        if(res.data.data.comintro == ""){
          that.setData({
            comintro: false,
          })
        }
        let pic = res.data.data.pic;
        let video = res.data.data.video;
        if(video){
          video = 'https://www.135k.com/addons/sd_humans/tp/public/uploads/video/' + video;
        }
        for (let k = 0, length = pic.length; k < length; k++) {
          pic[k] = 'https://www.135k.com/addons/sd_humans/tp/public/uploads/pic/'+pic[k]
        }
        that.setData({
          pic: pic,
          video: video
        })
        // console.log(that.data.pic)
      }
    })
    // 请求须知
    wx.request({
      url: app.data.Url+'job/need',
      success: function(data){
        let need = data.data.data;
        console.log(need,'need');
        let needShow = need.length;
        that.setData({
          need:need,
          needShow:needShow
        })
      }
    })
    //请求是否已收藏
    wx.request({
      url: app.data.Url + "collect/collected",
      data: {
        bid: 51,
        goodsid: id,
        uid: uid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data == 1) {
          that.setData({
            collected: 1
          })
        }
      }
    })
    //请求是否已申请
    wx.request({
      url: app.data.Url + "apply/applied",
      data: {
        job: id,
        uid: uid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data == 1) {
          that.setData({
            applied: 1
          })
        }
      }
    })
  },

  //预览图片
  img:function(e){
    let that = this;
    wx.previewImage({
      current: that.data.pic[e.currentTarget.dataset.index], // 当前显示图片的http链接
      urls: that.data.pic // 需要预览的图片http链接列表
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let id = this.data.id;
    wx.request({
      url: app.data.Url + "job/hotAdd",
      data: {
        id:id,
      },
      success: function(res){
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 拨打电话
  telPhone:function(e){
    let that = this;
    wx.request({
      url: app.data.Url + 'login/getUser1',
      data: {
        user: wx.getStorageSync('uid'),
      },
      method: "post",
      success: function (res) {
        if (res.data == 0) {
          wx.navigateTo({
            url: '../editor/editor'
          })
        } else {
          let phone = that.data.res.phone
          wx.makePhoneCall({
            phoneNumber: phone
          })
        }
      }
    })
  },
  // 收藏兼职
  shoucang:function(e){
    var that=this;
    wx.request({
      url: app.data.Url + 'apply/collect',
      data: {
        goodsid: e.currentTarget.id,
        uid: wx.getStorageSync('uid'),
        bid: 51,
      },
      method: 'post',
      success: function (res) {
        console.log(res);
        if (res.data == 1) {
          wx.showToast({
            title: '收藏成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          that.setData({
            collected: 1
          })
        } else if (res.data == 3) {
          wx.showToast({
            title: '取消收藏',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          that.setData({
            collected:0
          })
        } else {
          wx.showToast({
            title: '收藏失败',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }
      }
    })
  },
  // 申请兼职
  shenqing: function (e) {
    let that = this;
    wx.request({
      url: app.data.Url + 'login/getUser1',
      data: {
        user: wx.getStorageSync('uid'),
      },
      method: "post",
      success: function (res) {
        if(res.data == 0){
          wx.navigateTo({
            url: '../editor/editor'
          })
        } else {
          wx.request({
            url: app.data.Url + 'apply/apply',
            data: {
              job: e.currentTarget.id,
              user: wx.getStorageSync('uid'),
              business: 51,
            },
            method: 'post',
            success: function (res) {
              if (res.data == 1) {
                wx.showToast({
                  title: '申请成功',
                  icon: 'succes',
                  duration: 1000,
                  mask: true
                })
                that.setData({
                  applied: 1
                })
              } else if (res.data == 3) {
                wx.showToast({
                  title: '重复申请',
                  icon: 'succes',
                  duration: 1000,
                  mask: true
                })
              } else {
                wx.showToast({
                  title: '申请失败',
                  icon: 'succes',
                  duration: 1000,
                  mask: true
                })
              }
            }
          })
        }
      }
    }) 
  }
})